package com.zhiqianbao.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.zhiqianbao.R;
import com.zhiqianbao.entity.LoanApply;
import com.zhiqianbao.entity.LoanStatus;
import com.zhiqianbao.entity.Result;
import com.zhiqianbao.utils.CheckUtils;
import com.zhiqianbao.utils.Constants;
import com.zhiqianbao.utils.HttpUtils;
import com.zhiqianbao.utils.LianLianUtils;
import com.zhiqianbao.utils.LoanConstants;
import com.zhiqianbao.utils.WorthPurseUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import cn.trinea.android.common.util.StringUtils;

/**
 * Created by liang on 2016/1/27.
 */
public class ApplyProgressFragment extends LazyLoadFragment implements  View.OnClickListener{
    private static final int UPDATE_TEXT = 6;
    //进度查询相关view控件o
    ScrollView srcollviewContainerItem2;
    /**
     * 借款金额
     */
    TextView textViewBorrowBalanceContent;
    /**o
     * 借款期限
     */
    TextView textViewLifeOfLoanContent;
    /**
     * 提交申请时间显示
     */
    TextView sjTjsqText;
    /**
     * 综合评估进行中图片
     */
    ImageView jdZhpgjxzImg;
    /**
     * 综合评估进行中竖线
     */
    ImageView sxZhpgjxzImg;
    /**
     * 综合评估进行中时间
     */
    TextView sjZhpgjxzText;
    /**
     * 审核确认进行中图片
     */
    ImageView jdShqrjxzImg;
    /**
     * 标题
     */
    TextView jdShqrjxzTitle;
    /**
     * 审核确认进行中的竖线
     */
    ImageView sxShqrjxzImg;
    /**
     * 审核确认进行中的时间显示
     */
    TextView sjShqrjxzText;
    /**
     * 发放贷款资金中图片
     */
    ImageView jdFfjdzjzImg;
    /**
     * 发放贷款资金中竖线
     */
    ImageView sxFfjdzjzImg;
    /**
     * 发放贷款资金中时间显示
     */
    TextView sjFfjdzjzText;
    /**
     * 标题
     */
    TextView jdFfjdzjzTitle;
    /**
     * 已经完成图片
     */
    ImageView jdYwjImg;
    /**
     * 标题
     */
    TextView jdYwjTitle;
    /**
     * 取消时间
     */
    TextView sjYqxText;
    /**
     * 取消按钮
     */
    Button buttonCancelRequest;
    /**
     * 没有借款信息需要显示
     */
    TextView textviewDataNull;
    /**
     * 借款详情
     */
    RelativeLayout loanApplyInfoRelativeLayout;

    /**
     * 跳转认证按钮
     */
    Button buttonBank;
    TextView bankButtonMessageInfo;
    SwipeRefreshLayout swipRefresh;
    private Animation operatingAnim ;
    LoanApply loanApply;
    LoanStatus loanStateTime;
    ScrollView srcollview_container_item2;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    int underHeight;

    Handler applyProgressHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1 :
                    if (ApplyProgressFragment.this.isAdded()){
                        underHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 17, getResources().getDisplayMetrics());
                        int underWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 17, getResources().getDisplayMetrics());
                        int ohterHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 17, getResources().getDisplayMetrics());
                        int ohterWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 17, getResources().getDisplayMetrics());
                        int underLeft = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, getResources().getDisplayMetrics());
                        int ohterLeft = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, getResources().getDisplayMetrics());
                        LinearLayout.LayoutParams underMarginLeft = new LinearLayout.LayoutParams(underWidth, underHeight);
                        underMarginLeft.setMargins(underLeft, 0, 0, 0);
                        LinearLayout.LayoutParams ohterMarginLeft = new LinearLayout.LayoutParams(ohterWidth, ohterHeight);
                        ohterMarginLeft.setMargins(ohterLeft, 0, 0, 0);

                        //通过查询的进度详情，显示处理
                        loanApply = (LoanApply) msg.obj;
                        //如果不存在借款信息 显示没有借款
                        if (null == loanApply) {
                            textviewDataNull.setVisibility(View.VISIBLE);
                            srcollviewContainerItem2.setVisibility(View.GONE);
                            closeWaitDialog();
                            return;
                        }else {
                            textviewDataNull.setVisibility(View.GONE);
                            srcollviewContainerItem2.setVisibility(View.VISIBLE);
                            closeWaitDialog();
                        }
                        loanStateTime = loanApply.getLoanStateTime();

                        //显示借款信息
                        textViewBorrowBalanceContent.setText(loanApply.getAmount().intValue() + "");
                        textViewLifeOfLoanContent.setText(loanApply.getTerm() + "");
                        //状态
                        String loanState = loanApply.getState();
                        //判断状态，进行显示
                        //刚刚提交申请状态
                        if (LoanConstants.LOAN_APPLY_CONFIRM.equals(loanState) ||
                                LoanConstants.LOAN_AUDIT_WAITING.equals(loanState) ||
                                LoanConstants.LOAN_AUDIT_CONFIRM_FAIL.equals(loanState) ||
                                LoanConstants.LOAN_FINANCE_AUDIT_FAIL.equals(loanState)) {

                            //取消按钮显示状态
                            buttonCancelRequest.setBackgroundResource(R.drawable.btn_request_normal_background);
                            buttonCancelRequest.setClickable(true);
                            buttonCancelRequest.setTextColor(Color.parseColor("#D2422F"));



                            //显示提交时间
                            Long tjsqTime = loanStateTime.getApplyConfirmTime();
                            String tjsqTimeStr = sdf.format(new Date(tjsqTime));
                            sjTjsqText.setText(tjsqTimeStr);
                            //综合评估进行中 显示
                            jdZhpgjxzImg.setImageResource(R.drawable.i_img_first_info_confirm_underway_icon);
                            jdZhpgjxzImg.setLayoutParams(underMarginLeft);
                            sxZhpgjxzImg.setImageResource(R.drawable.i_img_apply_index_vertical_line_underway);
                            sjZhpgjxzText.setText("");
                            //审核确认进行中 显示
                            jdShqrjxzImg.setImageResource(R.drawable.i_img_first_info_confirm_before_icon);
                            jdShqrjxzImg.setLayoutParams(ohterMarginLeft);
                            sxShqrjxzImg.setImageResource(R.drawable.i_img_apply_index_vertical_line_before);
                            sjShqrjxzText.setText("");
                            jdShqrjxzTitle.setTextColor(Color.parseColor("#ffD9D9D9"));
                            //发放贷款
                            jdFfjdzjzImg.setImageResource(R.drawable.i_img_first_info_confirm_before_icon);
                            jdFfjdzjzImg.setLayoutParams(ohterMarginLeft);
                            sxFfjdzjzImg.setImageResource(R.drawable.i_img_apply_index_vertical_line_before);
                            sjFfjdzjzText.setText("");
                            jdFfjdzjzTitle.setTextColor(Color.parseColor("#ffD9D9D9"));
                            //已完结
                            jdYwjImg.setImageResource(R.drawable.i_img_first_info_confirm_before_icon);
                            jdYwjTitle.setTextColor(Color.parseColor("#ffD9D9D9"));

                            //显示 审核确认进行中
                        } else if (LoanConstants.LOAN_AUDIT_SUCCESS.equals(loanState)) {

                            //取消按钮显示状态
                            buttonCancelRequest.setBackgroundResource(R.drawable.activity_apply_index_want_to_express_normal_icon);
                            buttonCancelRequest.setClickable(false);
                            buttonCancelRequest.setTextColor(Color.parseColor("#FFFFFF"));

                            //显示提交时间
                            Long tjsqTime = loanStateTime.getApplyConfirmTime();
                            String tjsqTimeStr = sdf.format(new Date(tjsqTime));
                            sjTjsqText.setText(tjsqTimeStr);
                            //综合评估进行中 显示
                            Long zhpgTime = loanStateTime.getAuditTime();
                            String zhpgTimeStr = sdf.format(new Date(zhpgTime));
                            jdZhpgjxzImg.setImageResource(R.drawable.i_img_first_info_confirm_underway_icon);

                            jdZhpgjxzImg.setLayoutParams(ohterMarginLeft);
                            sxZhpgjxzImg.setImageResource(R.drawable.i_img_apply_index_vertical_line_apfter);
                            sjZhpgjxzText.setText(zhpgTimeStr);
                            //审核确认进行中 显示
                            jdShqrjxzImg.setImageResource(R.drawable.i_img_first_info_confirm_underway_icon);
                            jdShqrjxzImg.setLayoutParams(underMarginLeft);
                            sxShqrjxzImg.setImageResource(R.drawable.i_img_apply_index_vertical_line_underway);
                            sjShqrjxzText.setText("");
                            jdShqrjxzTitle.setTextColor(Color.parseColor("#ff666666"));
                            //发放贷款
                            jdFfjdzjzImg.setImageResource(R.drawable.i_img_first_info_confirm_before_icon);

                            jdFfjdzjzImg.setLayoutParams(ohterMarginLeft);
                            sxFfjdzjzImg.setImageResource(R.drawable.i_img_apply_index_vertical_line_before);
                            sjFfjdzjzText.setText("");
                            jdFfjdzjzTitle.setTextColor(Color.parseColor("#ffD9D9D9"));
                            //已完结
                            jdYwjImg.setImageResource(R.drawable.i_img_first_info_confirm_before_icon);
                            jdYwjTitle.setTextColor(Color.parseColor("#ffD9D9D9"));
                            //显示 发放贷款资金中
                        } else if (LoanConstants.LOAN_AUDIT_CONFIRM_SUCCESS.equals(loanState) ||
                                LoanConstants.LOAN_FINANCE_AUDIT_SUCCESS.equals(loanState)) {

                            //取消按钮显示状态
                            buttonCancelRequest.setBackgroundResource(R.drawable.activity_apply_index_want_to_express_normal_icon);
                            buttonCancelRequest.setClickable(false);
                            buttonCancelRequest.setTextColor(Color.parseColor("#FFFFFF"));

                            //显示提交时间
                            Long tjsqTime = loanStateTime.getApplyConfirmTime();
                            String tjsqTimeStr = sdf.format(new Date(tjsqTime));
                            sjTjsqText.setText(tjsqTimeStr);
                            //综合评估进行中 显示
                            Long zhpgTime = loanStateTime.getAuditTime();
                            String zhpgTimeStr = sdf.format(new Date(zhpgTime));
                            jdZhpgjxzImg.setImageResource(R.drawable.i_img_first_info_confirm_underway_icon);

                            jdZhpgjxzImg.setLayoutParams(ohterMarginLeft);
                            sxZhpgjxzImg.setImageResource(R.drawable.i_img_apply_index_vertical_line_apfter);
                            sjZhpgjxzText.setText(zhpgTimeStr);
                            //审核确认进行中 显示
                            Long shqrTime = loanStateTime.getAuditConfirmTime();
                            String shqrTimeStr = sdf.format(new Date(shqrTime));
                            jdShqrjxzImg.setImageResource(R.drawable.i_img_first_info_confirm_underway_icon);

                            jdShqrjxzImg.setLayoutParams(ohterMarginLeft);
                            sxShqrjxzImg.setImageResource(R.drawable.i_img_apply_index_vertical_line_apfter);
                            sjShqrjxzText.setText(shqrTimeStr);
                            jdShqrjxzTitle.setTextColor(Color.parseColor("#ff666666"));
                            //发放贷款
                            jdFfjdzjzImg.setImageResource(R.drawable.i_img_first_info_confirm_underway_icon);

                            jdFfjdzjzImg.setLayoutParams(underMarginLeft);
                            sxFfjdzjzImg.setImageResource(R.drawable.i_img_apply_index_vertical_line_underway);
                            sjFfjdzjzText.setText("");
                            jdFfjdzjzTitle.setTextColor(Color.parseColor("#ff666666"));
                            //已完结
                            jdYwjImg.setImageResource(R.drawable.i_img_first_info_confirm_before_icon);
                            jdYwjTitle.setTextColor(Color.parseColor("#ffD9D9D9"));

                            //显示 已经完结
                        } else if (LoanConstants.LOAN_GRANT_FUNDS_SUCCESS.equals(loanState) ||
                                LoanConstants.LOAN_FINISH.equals(loanState)) {

                            //取消按钮显示状态
                            buttonCancelRequest.setBackgroundResource(R.drawable.activity_apply_index_want_to_express_normal_icon);
                            buttonCancelRequest.setClickable(false);
                            buttonCancelRequest.setTextColor(Color.parseColor("#FFFFFF"));
                            try{
                                //显示提交时间
                                Long tjsqTime = loanStateTime.getApplyConfirmTime();
                                String tjsqTimeStr = sdf.format(new Date(tjsqTime));
                                sjTjsqText.setText(tjsqTimeStr);
                                //综合评估进行中 显示
                                Long zhpgTime = loanStateTime.getAuditTime();
                                String zhpgTimeStr = sdf.format(new Date(zhpgTime));
                                jdZhpgjxzImg.setImageResource(R.drawable.i_img_first_info_confirm_underway_icon);

                                jdZhpgjxzImg.setLayoutParams(ohterMarginLeft);
                                sxZhpgjxzImg.setImageResource(R.drawable.i_img_apply_index_vertical_line_apfter);
                                sjZhpgjxzText.setText(zhpgTimeStr);
                                //审核确认进行中 显示
                                Long shqrTime = loanStateTime.getAuditConfirmTime();
                                String shqrTimeStr = sdf.format(new Date(shqrTime));
                                jdShqrjxzImg.setImageResource(R.drawable.i_img_first_info_confirm_underway_icon);

                                jdShqrjxzImg.setLayoutParams(ohterMarginLeft);
                                sxShqrjxzImg.setImageResource(R.drawable.i_img_apply_index_vertical_line_apfter);
                                sjShqrjxzText.setText(shqrTimeStr);
                                jdShqrjxzTitle.setTextColor(Color.parseColor("#ff666666"));
                                //发放贷款
                                Long ffdkTime = loanStateTime.getGrantFundsTime();
                                String ffdkTimeStr = sdf.format(new Date(ffdkTime));
                                jdFfjdzjzImg.setImageResource(R.drawable.i_img_first_info_confirm_underway_icon);

                                jdFfjdzjzImg.setLayoutParams(ohterMarginLeft);
                                sxFfjdzjzImg.setImageResource(R.drawable.i_img_apply_index_vertical_line_apfter);
                                sjFfjdzjzText.setText(ffdkTimeStr);
                                jdFfjdzjzTitle.setTextColor(Color.parseColor("#ff666666"));
                                //已完结
                                jdYwjImg.setImageResource(R.drawable.i_img_first_info_confirm_underway_icon);
                                jdYwjTitle.setTextColor(Color.parseColor("#ff666666"));
                            }catch (Exception e){

                            }

                            //显示不正常完结
                        } else if (LoanConstants.LOAN_APPLY_CANCEL.equals(loanState) ||
                                LoanConstants.LOAN_AUDIT_FAIL.equals(loanState) ||
                                LoanConstants.LOAN_AUDIT_CANCEL.equals(loanState)) {

                            //取消按钮显示状态
                            buttonCancelRequest.setBackgroundResource(R.drawable.activity_apply_index_want_to_express_normal_icon);
                            buttonCancelRequest.setClickable(false);
                            buttonCancelRequest.setTextColor(Color.parseColor("#FFFFFF"));

                            //显示提交时间
                            Long tjsqTime = loanStateTime.getApplyConfirmTime();
                            String tjsqTimeStr = sdf.format(new Date(tjsqTime));
                            sjTjsqText.setText(tjsqTimeStr);
                            //综合评估进行中 显示
                            jdZhpgjxzImg.setImageResource(R.drawable.i_img_first_info_confirm_underway_icon);

                            jdZhpgjxzImg.setLayoutParams(underMarginLeft);
                            sxZhpgjxzImg.setImageResource(R.drawable.i_img_apply_index_vertical_line_underway);
                            sjZhpgjxzText.setText("");
                            //审核确认进行中 显示
                            jdShqrjxzImg.setImageResource(R.drawable.i_img_first_info_confirm_before_icon);

                            jdShqrjxzImg.setLayoutParams(ohterMarginLeft);
                            sxShqrjxzImg.setImageResource(R.drawable.i_img_apply_index_vertical_line_before);
                            sjShqrjxzText.setText("");
                            jdShqrjxzTitle.setTextColor(Color.parseColor("#ffD9D9D9"));
                            //发放贷款
                            jdFfjdzjzImg.setImageResource(R.drawable.i_img_first_info_confirm_before_icon);

                            jdFfjdzjzImg.setLayoutParams(ohterMarginLeft);
                            sxFfjdzjzImg.setImageResource(R.drawable.i_img_apply_index_vertical_line_before);
                            sjFfjdzjzText.setText("");
                            jdFfjdzjzTitle.setTextColor(Color.parseColor("#ffD9D9D9"));
                            //已完结
                            jdYwjImg.setImageResource(R.drawable.i_img_first_info_confirm_underway_icon);
                            jdYwjTitle.setTextColor(Color.parseColor("#ff666666"));
                            //用户取消
                            if (LoanConstants.LOAN_APPLY_CANCEL.equals(loanState)) {
                                Long cancelTime = loanStateTime.getCancelTime();
                                if (cancelTime != null) {
                                    String cancelTimeStr = sdf.format(new Date(cancelTime));
                                    sjYqxText.setText(cancelTimeStr);
                                }
                                jdYwjTitle.setText("申请已取消");
                                sxZhpgjxzImg.setImageResource(R.drawable.i_img_apply_index_vertical_line_before);
                            } else if (LoanConstants.LOAN_AUDIT_CANCEL.equals(loanState)){
                                Long cancelTime = loanStateTime.getCancelTime();
                                if (cancelTime != null) {
                                    String cancelTimeStr = sdf.format(new Date(cancelTime));
                                    sjYqxText.setText(cancelTimeStr);
                                }
                                jdYwjTitle.setText("审核取消");
                                sxZhpgjxzImg.setImageResource(R.drawable.i_img_apply_index_vertical_line_before);
                            } else if (LoanConstants.LOAN_AUDIT_FAIL.equals(loanState)) {
                                Long cancelTime = loanStateTime.getCancelTime();
                                if (cancelTime != null) {
                                    String cancelTimeStr = sdf.format(new Date(cancelTime));
                                    sjYqxText.setText(cancelTimeStr);
                                }
                                jdYwjTitle.setText("审核拒绝");
                                sxZhpgjxzImg.setImageResource(R.drawable.i_img_apply_index_vertical_line_before);
                            } else {
                                sjYqxText.setText("");
                                jdYwjTitle.setText("已完结");
                                sxZhpgjxzImg.setImageResource(R.drawable.i_img_apply_index_vertical_line_before);
                            }
                            closeWaitDialog();
                        }
                        closeWaitDialog();

                    }
                    break;
                case 0 :
                    showShortToast1(Constants.HTTP_ERROR_MESSAGE);
                    closeWaitDialog();
                    break;
                case UPDATE_TEXT:
                    String obj = (String) msg.obj;
                    Toast.makeText(getActivity(), obj, Toast.LENGTH_SHORT).show();
                    closeWaitDialog();
                    break;
            }
            closeWaitDialog();
            swipRefresh.setRefreshing(false);
        }
    };

    Handler cancelLoanApplyHandle = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0 :
                    showShortToast1(Constants.HTTP_ERROR_MESSAGE);
                    break;
                case 1 :
                    Result data = (Result) msg.obj;
                    if(data!=null) {
                        if (data.getCode().equals(Constants.DEFAULT_SUCCESS_CODE)) {
                            showShortToast1(data.getMsg());
                            checkLoanApply();
                        } else {
                            showShortToast1(data.getMsg());
                        }
                    }
                    break;
            }
            swipRefresh.setRefreshing(false);
        }
    };

    private void checkLoanApply() {
        if (Constants.LOGIN_USER == null) {
            Intent loginIntent = new Intent(getActivity(),
                    WorthPurseUtil.getZhiddActivity().getClass());
            startActivity(loginIntent);
            getActivity().finish();
        }
        else{
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Message message = new Message();
                    Map<String, String> param = new HashMap<>();
                    param.put("token", Constants.LOGIN_USER != null ? Constants.LOGIN_USER.getToken() : "");
                    param.put("userId", Constants.LOGIN_USER != null ? Constants.LOGIN_USER.getId() + "" : "");
                    String result = HttpUtils.httpPostString(Constants.API_ROOT + "/loan/getNewLoanApply", param, getActivity());
                    if (StringUtils.isBlank(result)) {
                        message.what = 0;
                    } else {
                        message.what = 1;
                        Result<LoanApply> data = Result.fromJson(result, LoanApply.class);
                        message.obj = data.getData();
                    }
                    applyProgressHandler.sendMessage(message);

                }
            }).start();
        }
    }


    private void cancelLoanApply() {
        if (Constants.LOGIN_USER == null) {
            Intent loginIntent = new Intent(getActivity(),
                    WorthPurseUtil.getZhiddActivity().getClass());
            startActivity(loginIntent);
            getActivity().finish();
        }
        else {
            //调用取消按钮接口
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Message cancelMessage = new Message();
                    Map<String, String> param = new HashMap<>();
                    param.put("token", Constants.LOGIN_USER != null ? Constants.LOGIN_USER.getToken() : "");
                    param.put("userId", Constants.LOGIN_USER != null ? Constants.LOGIN_USER.getId() + "" : "");
                    param.put("loanApplyId", loanApply!= null ? loanApply.getId() + "" : "");
                    String result = HttpUtils.httpPostString(Constants.API_ROOT + "/loan/cancelRegistration", param, getActivity());

                    if (StringUtils.isBlank(result)) {
                        cancelMessage.what = 0;
                    } else {
                        cancelMessage.what = 1;
                        Result data = Result.fromJson(result, HashMap.class);
                        cancelMessage.obj = data;
                    }
                    cancelLoanApplyHandle.sendMessage(cancelMessage);
                }
            }).start();
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup  container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_progess_apply_tab, null);
        initView(view);
        return view;
    }

    private void initView(View view) {
        //进度查询相关view控件
        srcollviewContainerItem2 = (ScrollView) view.findViewById(R.id.srcollview_container_item2);
        /**
         * 借款金额
         */
        textViewBorrowBalanceContent = (TextView) view.findViewById(R.id.textView_borrow_balance_content);
        /**
         * 借款期限
         */
        textViewLifeOfLoanContent = (TextView) view.findViewById(R.id.textView_life_of_loan_content);
        /**
         * 提交申请时间显示
         */
        sjTjsqText = (TextView) view.findViewById(R.id.sj_tjsq_text);
        /**
         * 综合评估进行中图片
         */
        jdZhpgjxzImg = (ImageView) view.findViewById(R.id.jd_zhpgjxz_img);
        /**
         * 综合评估进行中竖线
         */
        sxZhpgjxzImg = (ImageView) view.findViewById(R.id.sx_zhpgjxz_img);
        /**
         * 综合评估进行中时间
         */
        sjZhpgjxzText = (TextView) view.findViewById(R.id.sj_zhpgjxz_text);
        /**
         * 审核确认进行中图片
         */
        jdShqrjxzImg = (ImageView) view.findViewById(R.id.jd_shqrjxz_img);
        /**
         * 标题
         */
        jdShqrjxzTitle = (TextView) view.findViewById(R.id.jd_shqrjxz_title);
        /**
         * 审核确认进行中的竖线
         */
        sxShqrjxzImg = (ImageView) view.findViewById(R.id.sx_shqrjxz_img);
        /**
         * 审核确认进行中的时间显示
         */
        sjShqrjxzText = (TextView) view.findViewById(R.id.sj_shqrjxz_text);
        /**
         * 发放贷款资金中图片
         */
        jdFfjdzjzImg = (ImageView) view.findViewById(R.id.jd_ffjdzjz_img);
        /**
         * 发放贷款资金中竖线
         */
        sxFfjdzjzImg = (ImageView) view.findViewById(R.id.sx_ffjdzjz_img);
        /**
         * 发放贷款资金中时间显示
         */
        sjFfjdzjzText = (TextView) view.findViewById(R.id.sj_ffjdzjz_text);
        /**
         * 标题
         */
        jdFfjdzjzTitle = (TextView) view.findViewById(R.id.jd_ffjdzjz_title);
        /**
         * 已经完成图片
         */
        jdYwjImg = (ImageView) view.findViewById(R.id.jd_ywj_img);
        /**
         * 标题
         */
        jdYwjTitle = (TextView) view.findViewById(R.id.jd_ywj_title);

        sjYqxText = (TextView) view.findViewById(R.id.sj_yqx_text);
        /**
         * 取消按钮
         */
        buttonCancelRequest = (Button) view.findViewById(R.id.button_cancel_request);
        /**
         * 没有借款信息需要显示
         */
        textviewDataNull = (TextView) view.findViewById(R.id.textview_data_null);
        /**
         * 借款详情
         */
        loanApplyInfoRelativeLayout = (RelativeLayout) view.findViewById(R.id.loan_apply_info_relativeLayout);
        swipRefresh= (SwipeRefreshLayout) view.findViewById(R.id.swipRefresh);
        swipRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //查询借款进度接口
                if(Constants.LOGIN_USER!=null) {//!!!!!暂时性修改判断
                    checkLoanApply();
                    //查询银行卡认证状态
                    // checkShanxinState();
                }
            }
        });
        buttonBank = (Button) view.findViewById(R.id.button_bank);

        bankButtonMessageInfo = (TextView) view.findViewById(R.id.bank_button_message_info);
        srcollview_container_item2= (ScrollView) view.findViewById(R.id.srcollview_container_item2);

        if (srcollview_container_item2 != null) {
            srcollview_container_item2.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
                @Override
                public void onScrollChanged() {
                    if (swipRefresh != null) {
                        swipRefresh.setEnabled(srcollview_container_item2.getScrollY() == 0);
                    }
                }
            });
        }
        loanApplyInfoRelativeLayout.setOnClickListener(this);
        buttonCancelRequest.setOnClickListener(this);
        buttonBank.setOnClickListener(this);

    }

    boolean isUpdated = false;
    boolean isChecked = false;
    boolean isVisible = false;

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isVisible = isVisibleToUser;
        Log.i(getClass().getSimpleName(), "visibleHint");
        if (isVisibleToUser && !isUpdated) {
            if (isChecked) {
                return;
            }
            //对照ios功能
            checkLoanApply();
            //查询银行卡认证状态
            checkShanxinState();
        } else {
            isChecked = false;
            isUpdated = false;
        }
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_progess_apply_tab ;
    }

    @Override
    protected void lazyLoad() {
        if (Constants.LOGIN_USER!=null){
            checkLoanApply();
        }

        if (!isVisible) {
            return;
        }
        Log.i(getClass().getSimpleName(), "onResume");
        if (isChecked) {
            return;
        }
        if (!isUpdated) {
            //查询借款进度接口
//                checkLoanApply();
            isUpdated = true;
            isChecked = false;
        } else {
            isChecked = true;
            isUpdated = false;
        }
    }


    @Override
    public void onResume() {
        super.onResume();
//        if (Constants.LOGIN_USER!=null){
//            checkLoanApply();
//        }
//
//        if (!isVisible) {
//            return;
//        }
//        Log.i(getClass().getSimpleName(), "onResume");
//        if (isChecked) {
//            return;
//        }
//            if (!isUpdated) {
//                //查询借款进度接口
////                checkLoanApply();
//                isUpdated = true;
//                isChecked = false;
//            } else {
//                isChecked = true;
//                isUpdated = false;
//            }
    }
//        if(Constants.USER_STATE_REGIST_INFO.equals(userInfoState)||Constants.USER_STATE_FIRST_INFO.equals(userInfoState)||Constants.USER_STATE_SECOND_INFO.equals(userInfoState)) {
//            startActivity(new Intent(getActivity(), ProcessActivity.class));
//        }
//        if (!Constants.USER_STATE_REGIST_INFO.equals(userInfoState)|| !Constants.USER_STATE_FIRST_INFO.equals(userInfoState)|| !Constants.USER_STATE_SECOND_INFO.equals(userInfoState)&&!isUpdated) {
//            //查询借款进度接口
//            showWaitDialog();
//            checkLoanApply();
//            isUpdated = true;
//            isChecked = false;
//        } else {
//            isChecked = true;
//            isUpdated = false;
//        }


    @Override
    public void onPause() {
        Log.i(getClass().getSimpleName(), "onPause");
        isUpdated = false;
        super.onPause();
    }

    @Override
    public void onClick(View v) {
        if(R.id.button_cancel_request==v.getId()) {
            if (CheckUtils.checkUserState(getActivity())) {
                return;
            }
            cacelApplyLoapDialog();
        }
        else if(R.id.loan_apply_info_relativeLayout==v.getId()) {
            Intent loanApplyInfoIntent = new Intent(getActivity(), ApplyLoanInfoActivity.class);
            loanApplyInfoIntent.putExtra("loanApply", loanApply);
            CheckUtils.checkUserState(getActivity(), loanApplyInfoIntent);
        }
        else if(R.id.button_bank==v.getId()){
            //跳转到银行卡信息页面
                Intent bankCardInfoIntent = new Intent(getActivity(), BankCardInfoActivity.class);
                CheckUtils.checkUserState(getActivity(), bankCardInfoIntent);
        }
    }

    public void cacelApplyLoapDialog() {
        final AlertDialog builder = new AlertDialog.Builder(getActivity(), R.style.Dialog).create();
        builder.show();
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_cacel_applyloap_layout, null);
        builder.getWindow().setContentView(view);
        Button button = (Button) view.findViewById(R.id.positiveButton);
        Button cacelButton = (Button) view.findViewById(R.id.cacelButton);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancelLoanApply();
                builder.dismiss();
            }
        });

        cacelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                builder.dismiss();
            }
        });
    }

    /**
     * 检查连连签约状态
     */
    private void checkShanxinState() {
        LianLianUtils.checkLianlianBindCard(getActivity(), new LianLianUtils.Handler() {
            @Override
            public void process(String result) {
                Message checkShanxinStateMessage = new Message();
                checkShanxinStateMessage.what = -1;
                checkShanxinStateMessage.obj = result;
                checkSahnxinStateHandler.sendMessage(checkShanxinStateMessage);
            }

            @Override
            public void error(final String reason) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Message message = new Message();
                        message.what = UPDATE_TEXT;
                        message.obj = reason;
                        applyProgressHandler.sendMessage(message);
                    }
                });

            }
        });
        /*new Thread(new Runnable() {
            @Override
            public void run() {
                Message checkShanxinStateMessage = new Message();
                Map<String, String> param = new HashMap<>();
                param.put("userId", Constants.LOGIN_USER.getId().toString());
                param.put("token",  Constants.LOGIN_USER.getToken());

                //String result = HttpUtils.httpPostString(Constants.API_ROOT + "/user/shanqbState", param, getActivity());
                String result = HttpUtils.httpPostString(Constants.API_ROOT + "/loan/check/lianlian/state", param, getActivity());

                if (StringUtils.isBlank(result)) {
                    checkShanxinStateMessage.what = 0;
                } else {
                    Result<String> data = Result.fromJson(result, String.class);
                    String state = "show";
                    switch (data.getCode()){
                        case "0":
                            state = "hidden";
                        break;
                    }
                    checkShanxinStateMessage.what = 1;
                    checkShanxinStateMessage.obj = state;
                }

                checkSahnxinStateHandler.sendMessage(checkShanxinStateMessage);

            }
        }).start();*/
    }
    Handler checkSahnxinStateHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case -1:
                    Object object = msg.obj;
                    String result = (String) object;
                    //已认证 隐藏认证按钮
                    if (StringUtils.isBlank(result) || LianLianUtils.CARD_STATUS_BIND.equals(result)) {
                        buttonBank.setVisibility(View.GONE);
                        bankButtonMessageInfo.setVisibility(View.GONE);
                        //未认证 显示认证按钮
                    } else {
                        buttonBank.setVisibility(View.VISIBLE);
                        bankButtonMessageInfo.setVisibility(View.VISIBLE);
                    }
//                    refreshList.clearAnimation();
                    break;
            }
        }
    };
}